#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
测试数据生成器 - LeetCode 134 加油站
根据md文档中的示例和边界条件生成测试用例
"""

import random

def generate_test_cases():
    """生成测试用例"""
    test_cases = []
    
    # 1. 基本示例 - 来自md文档示例1
    test_cases.append({
        "name": "示例1 - 有解情况",
        "gas": [1, 2, 3, 4, 5],
        "cost": [3, 4, 5, 1, 2],
        "expected": 3,
        "description": "从3号加油站出发可以绕一圈"
    })
    
    # 2. 基本示例 - 来自md文档示例2
    test_cases.append({
        "name": "示例2 - 无解情况",
        "gas": [2, 3, 4],
        "cost": [3, 4, 3],
        "expected": -1,
        "description": "无论从哪个加油站出发都无法绕一圈"
    })
    
    # 3. 边界情况 - 单个加油站
    test_cases.append({
        "name": "单个加油站 - 有解",
        "gas": [5],
        "cost": [3],
        "expected": 0,
        "description": "只有一个加油站，汽油足够"
    })
    
    test_cases.append({
        "name": "单个加油站 - 无解",
        "gas": [2],
        "cost": [5],
        "expected": -1,
        "description": "只有一个加油站，汽油不足"
    })
    
    # 4. 特殊情况 - 恰好够用
    test_cases.append({
        "name": "恰好够用",
        "gas": [3, 3, 4],
        "cost": [3, 4, 3],
        "expected": 2,
        "description": "从最后一个加油站出发恰好够用"
    })
    
    # 5. 所有加油站都一样
    test_cases.append({
        "name": "所有站点相同 - 有解",
        "gas": [2, 2, 2, 2],
        "cost": [1, 1, 1, 1],
        "expected": 0,
        "description": "所有加油站条件相同，任意起点都可以"
    })
    
    test_cases.append({
        "name": "所有站点相同 - 无解",
        "gas": [1, 1, 1],
        "cost": [2, 2, 2],
        "expected": -1,
        "description": "所有加油站条件相同，任意起点都不可以"
    })
    
    # 6. 大多数站点无油
    test_cases.append({
        "name": "大多数站点无油",
        "gas": [0, 0, 0, 10],
        "cost": [1, 1, 1, 7],
        "expected": 3,
        "description": "只有最后一个站有足够的油"
    })
    
    # 7. 复杂情况 - 需要跳跃优化
    test_cases.append({
        "name": "需要跳跃优化的情况",
        "gas": [1, 2, 3, 4, 5, 6],
        "cost": [6, 5, 4, 3, 2, 1],
        "expected": 5,
        "description": "前面几个站点都不足以支撑到下一站"
    })
    
    # 8. 较大规模测试
    test_cases.append({
        "name": "中等规模测试",
        "gas": [5, 1, 2, 3, 4] * 4,  # 20个站点
        "cost": [4, 4, 1, 5, 1] * 4,
        "expected": 4,  # 需要算法验证
        "description": "中等规模的环路测试"
    })
    
    return test_cases

def generate_random_test_case(size=10, max_val=100):
    """生成随机测试用例"""
    gas = [random.randint(0, max_val) for _ in range(size)]
    cost = [random.randint(0, max_val) for _ in range(size)]
    
    return {
        "name": f"随机测试 - 大小{size}",
        "gas": gas,
        "cost": cost,
        "expected": None,  # 需要算法计算
        "description": f"随机生成的{size}个加油站测试用例"
    }

def generate_edge_cases():
    """生成边界情况测试用例"""
    edge_cases = []
    
    # 最小规模
    edge_cases.append({
        "name": "最小规模 - 有解",
        "gas": [1],
        "cost": [0],
        "expected": 0,
        "description": "n=1，有解"
    })
    
    # 总油量刚好等于总消耗
    edge_cases.append({
        "name": "总量平衡",
        "gas": [1, 2, 3],
        "cost": [2, 2, 2],
        "expected": 2,
        "description": "总油量等于总消耗，但分布不均"
    })
    
    # 第一个站点就是答案
    edge_cases.append({
        "name": "第一个站点即答案",
        "gas": [10, 1, 1],
        "cost": [1, 5, 5],
        "expected": 0,
        "description": "第一个加油站就是正确答案"
    })
    
    # 最后一个站点是答案
    edge_cases.append({
        "name": "最后一个站点即答案",
        "gas": [1, 1, 10],
        "cost": [5, 5, 1],
        "expected": 2,
        "description": "最后一个加油站是正确答案"
    })
    
    return edge_cases

def save_test_cases_to_file(filename="test_data.py"):
    """将测试用例保存到文件"""
    all_cases = generate_test_cases() + generate_edge_cases()
    
    # 添加几个随机测试用例
    for size in [5, 10, 20]:
        all_cases.append(generate_random_test_case(size))
    
    content = "#!/usr/bin/env python3\n# -*- coding: utf-8 -*-\n"
    content += '"""自动生成的测试数据"""\n\n'
    content += "TEST_CASES = [\n"
    
    for case in all_cases:
        content += "    {\n"
        content += f"        'name': '{case['name']}',\n"
        content += f"        'gas': {case['gas']},\n"
        content += f"        'cost': {case['cost']},\n"
        content += f"        'expected': {case['expected']},\n"
        content += f"        'description': '{case['description']}'\n"
        content += "    },\n"
    
    content += "]\n"
    
    with open(filename, 'w', encoding='utf-8') as f:
        f.write(content)
    
    return all_cases

if __name__ == "__main__":
    print("生成加油站问题测试用例...")
    test_cases = generate_test_cases()
    edge_cases = generate_edge_cases()
    
    print(f"生成了 {len(test_cases)} 个基本测试用例")
    print(f"生成了 {len(edge_cases)} 个边界测试用例")
    
    # 显示部分测试用例
    print("\n部分测试用例预览:")
    for i, case in enumerate(test_cases[:3]):
        print(f"{i+1}. {case['name']}")
        print(f"   gas: {case['gas']}")
        print(f"   cost: {case['cost']}")
        print(f"   expected: {case['expected']}")
        print(f"   描述: {case['description']}\n")
    
    # 保存到文件
    all_cases = save_test_cases_to_file()
    print(f"所有测试用例已保存到 test_data.py，共 {len(all_cases)} 个用例")